<?php

require_once ('./main/inc/global.inc.php');

function get_news($news_id) {
    $news = Database::get_newsletter_by_id($news_id);
    return json_encode($news);
}

function get_newsletters($count) {
    $newsletters = Database::get_all_newsletters();
    if (count($newsletters) == $count) {
        return json_encode(array("no_change" => true));
    }
    return json_encode($newsletters);
}

function install_plugin($user_id, $plugin_id) {
    Database::install_plugin($user_id, $plugin_id);
    return json_encode(array($user_id, $plugin_id));
}

function uninstall_plugin($user_id, $plugin_id) {
    Database::uninstall_plugin($user_id, $plugin_id);
    return json_encode(array($user_id, $plugin_id));
}

function handle_request() {
    $request = $_GET['request'];
    if ($request  == 'get_newsletters') {
        echo get_newsletters($_GET['count']);
    } else if ($request == 'get_news') {
        echo get_news($_GET['id']);
    } else if ($request == 'install_plugin') {
        echo install_plugin($_GET['user_id'], $_GET['plugin_id']);
    } else if ($request == 'uninstall_plugin') {
        echo uninstall_plugin($_GET['user_id'], $_GET['plugin_id']);
    }
}

handle_request();
?>
